package com.yiwenup.leetcode.hot;

/**
 * https://leetcode-cn.com/problems/product-of-array-except-self/
 **/
public class No0238 {
    /**
     * 执行用时：1 ms, 在所有 Java 提交中击败了100.00%的用户
     * 内存消耗：49.6 MB, 在所有 Java 提交中击败了31.80%的用户
     */
    public int[] productExceptSelf(int[] nums) {

        int[] res = new int[nums.length];

        int left = 1;
        int right = 1;

        for (int i = 0; i < nums.length; i++) {
            res[i] = left;
            left *= nums[i];
        }

        for (int i = nums.length - 1; i >= 0; i--) {
            res[i] *= right;
            right *= nums[i];
        }

        return res;
    }
}
